/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package View.gestion;

import Controller.ModeloC;
import Model.ModeloM;
import Util.ArrayListTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author LISSET
 */
public class frmBuscar extends javax.swing.JDialog {

    /**
     * Creates new form frmBuscar
     */
    public frmBuscar(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setResizable(false);
        setLocationRelativeTo(null);
        configurarTabla();
        tblPrenda.setRowHeight(35);
        cargarTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtBuscar = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblPrenda = new javax.swing.JTable();

        jLabel1.setText("jLabel1");

        jTextField1.setText("jTextField1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Prendas");

        jLabel2.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Buscar2.png"))); // NOI18N
        jLabel2.setText("Buscar:");

        txtBuscar.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        txtBuscar.setPreferredSize(new java.awt.Dimension(6, 35));
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarKeyReleased(evt);
            }
        });

        tblPrenda.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        tblPrenda.setModel(new ArrayListTableModel(
            new Object [][] {},
            new String [] {"  NRO"," DESCRIPCIÓN"," TALLA","   PRECIO","PRECIO MAYOR",""}
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class,java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false,false,false,false,false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblPrenda.getTableHeader().setReorderingAllowed(false);
        tblPrenda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblPrendaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblPrenda);
        tblPrenda.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ProjSave");
        tblPrenda.getActionMap().put("ProjSave", new AbstractAction()
            {
                public void actionPerformed(ActionEvent e)
                {
                    int fila = tblPrenda.getSelectedRow();
                    if ((fila > -1))
                    {
                        getModelo();
                    }
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(27, 27, 27))
                .addGroup(layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(21, 21, 21))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents
    private void buscarModelo() {
        String datoModelo = txtBuscar.getText().trim();
        ModeloC oModeloB = new ModeloC();
        List<ModeloM> oModeloMs = oModeloB.consultarModelo(datoModelo);
        cargarTabla(oModeloMs);

    }
    
    private void cargarTabla(List<ModeloM>  oModeloM)
    {
        limpiarTabla();
        ArrayList oList;
       if (oModeloM != null) {
            int size = oModeloM.size();
            for (int a = 0; a < size; a++) {
                oList = new ArrayList();
                oList.add(a + 1);
                oList.add(" " + oModeloM.get(a).getNomModelo());
                oList.add(oModeloM.get(a).getTalla());
                oList.add(oModeloM.get(a).getPrecio());
                oList.add(oModeloM.get(a).getPrecioMayor());
                oList.add(oModeloM.get(a));
                ((ArrayListTableModel) tblPrenda.getModel()).addRow(oList);
            }
        }
    }
    
    private void limpiarTabla()
    {
        ArrayListTableModel oModelTbl=(ArrayListTableModel)tblPrenda.getModel();
        int i=oModelTbl.getRowCount();
        for(int a=0;a<i;a++)
        {
           oModelTbl.removeRow(0);
        }
    }
    
    private void txtBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyReleased
        
            buscarModelo();
        
        if (evt.getKeyCode() == evt.VK_DOWN) {
            int size = tblPrenda.getRowCount();
            if (size > 0) {
                tblPrenda.requestFocus();
                tblPrenda.setRowSelectionInterval(0, 0);
            }
        }

    }//GEN-LAST:event_txtBuscarKeyReleased

    private void tblPrendaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblPrendaMouseClicked
          if(evt.getClickCount()==2) {
            int FilaSeleccionada =tblPrenda.getSelectedRow();
            if(FilaSeleccionada!=-1) {
                this.getModelo();
            }
        }
    }//GEN-LAST:event_tblPrendaMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTable tblPrenda;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables

    private void cargarTabla() {
        ModeloC oC = new ModeloC();
        List<ModeloM> oMs = oC.consultarModelo("");
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        ArrayList oList;
        //limpiarTablaEmisor();
        if (oMs != null) {
            int size = oMs.size();
            for (int a = 0; a < size; a++) {
                oList = new ArrayList();
                oList.add(a + 1);
                oList.add(" " + oMs.get(a).getNomModelo());
                oList.add(oMs.get(a).getTalla());
                oList.add(oMs.get(a).getPrecio());
                oList.add(oMs.get(a).getPrecioMayor());
                oList.add(oMs.get(a));
                ((ArrayListTableModel) tblPrenda.getModel()).addRow(oList);
            }
        }
    }

    private void configurarTabla() {
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.CENTER);

        tblPrenda.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblPrenda.getColumnModel().getColumn(2).setCellRenderer(tcr);
        tblPrenda.getColumnModel().getColumn(3).setCellRenderer(tcr);

        tblPrenda.getColumnModel().getColumn(0).setMinWidth(50);
        tblPrenda.getColumnModel().getColumn(0).setPreferredWidth(50);
        tblPrenda.getColumnModel().getColumn(0).setMaxWidth(50);
        tblPrenda.getColumnModel().getColumn(2).setMinWidth(50);
        tblPrenda.getColumnModel().getColumn(2).setPreferredWidth(50);
        tblPrenda.getColumnModel().getColumn(2).setMaxWidth(50);
        tblPrenda.getColumnModel().getColumn(3).setMinWidth(80);
        tblPrenda.getColumnModel().getColumn(3).setPreferredWidth(80);
        tblPrenda.getColumnModel().getColumn(3).setMaxWidth(80);
        tblPrenda.getColumnModel().getColumn(4).setMinWidth(90);
        tblPrenda.getColumnModel().getColumn(4).setPreferredWidth(90);
        tblPrenda.getColumnModel().getColumn(4).setMaxWidth(90);
        tblPrenda.getColumnModel().getColumn(5).setMinWidth(0);
        tblPrenda.getColumnModel().getColumn(5).setPreferredWidth(0);
        tblPrenda.getColumnModel().getColumn(5).setMaxWidth(0);
    }
    
     public ModeloM getModelo()
    {
        int selectedRow=tblPrenda.getSelectedRow();
        ModeloM oModeloM=null;
        if(selectedRow!=-1)
        {
            oModeloM=(ModeloM)tblPrenda.getValueAt(selectedRow,5);
            dispose();
        }
        return oModeloM;
    }
}
